package xyz.worldzhile.choice.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.worldzhile.choice.base.RequestVo;
import xyz.worldzhile.choice.base.RespHead;
import xyz.worldzhile.choice.base.ResponseVo;
import xyz.worldzhile.choice.dto.getSubjectReqDTO;
import xyz.worldzhile.choice.entity.Subject;
import xyz.worldzhile.choice.pre.QuestionLibrary;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/subject")
@Api(tags = "题目管理")
public class SubjectController {

    @Autowired
    private QuestionLibrary questionLibrary;


    @ApiOperation("根据题目序号获取题目")
    @PostMapping("get")
    public ResponseVo<Subject> getSubject(@Validated @RequestBody RequestVo<getSubjectReqDTO> requestVo) {

        getSubjectReqDTO body = requestVo.getBody();
        Integer number = body.getNumber();
        ResponseVo<Subject> subjectResponseVo = new ResponseVo<>();
        Subject subject = new Subject();
        HashMap<Integer, Subject> list = questionLibrary.getList();
        Set<Map.Entry<Integer, Subject>> entries = list.entrySet();
        entries.forEach(one->{

            if (one.getKey().intValue()==number){
                Subject subjectRes = list.get(one.getKey());
                BeanUtils.copyProperties(subjectRes,subject);
            }
        });
        subjectResponseVo.setBody(subject);
        if (subject.getNumber()==null){
            RespHead respHead = new RespHead();
            respHead.setCode("999");
            respHead.setMessage("未找到相应题目呀");
            subjectResponseVo.setHead(respHead);
        }
        return subjectResponseVo;
    }
}
